package cn.schoolwow.ams.module.block.listBlock.clazzBlock.initial.flow;

import cn.schoolwow.ams.domain.block.list.clazz.ClazzListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.clazz.ClazzListBlockOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CheckClazzListBlockOptionFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        Map<String, ClazzListBlockConfigAware> map = applicationContext.getBeansOfType(ClazzListBlockConfigAware.class);

        if(map.isEmpty()){
            flowContext.brokenCurrentFlow("无ClazzListBlock配置类列表信息");
        }
        List<ClazzListBlockOption> clazzListBlockOptionList = new ArrayList<>();
        for(ClazzListBlockConfigAware clazzListBlockConfigAware : map.values()){
            ClazzListBlockOption clazzListBlockOption = clazzListBlockConfigAware.getAMSBlockConfig();
            clazzListBlockOption.routerBlock = clazzListBlockConfigAware;

            String className = clazzListBlockConfigAware.getClass().getName();
            CheckInstance.newCheck()
                    .notNullCheck(clazzListBlockOption, "clazz表配置,涉及类名:["+className+"]")
                    .notEmptyCheck(clazzListBlockOption.projectName, "项目名称,涉及类名:["+className+"]")
                    .notEmptyCheck(clazzListBlockOption.displayName, "表显示名称,涉及类名:["+className+"]")
                    .notNullCheck(clazzListBlockOption.requestClass, "请求类,涉及类名:["+className+"]")
                    .notNullCheck(clazzListBlockOption.responseClass, "响应类,涉及类名:["+className+"]")
                    .notEmptyCheck(clazzListBlockOption.clazzName, "唯一AMS类名,涉及类名:["+className+"]");
            clazzListBlockOptionList.add(clazzListBlockOption);
        }
        flowContext.putContextData("clazzListBlockOptionList", clazzListBlockOptionList);
    }

    @Override
    public String name() {
        return "检查ClazzListBlockOption对象列表";
    }
}
